package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.utils.SMSUtils;
import com.itheima.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 发送验证码功能
     * @param user
     * @param request
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpServletRequest request){
        String phone = user.getPhone();
        //对phone进行健壮性判断
        if(StringUtils.isNotEmpty(phone)){
            //生成随机验证码
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("生成的验证码为：{}",code);
            //发送短信验证码
            //SMSUtils.sendMessage("瑞吉点餐",code,phone,"");
            //将验证码存到session中，以便登陆时验证
            //request.getSession().setAttribute("code",code);
            //将生成的验证码存入redis数据库中，并设置生命周期
            redisTemplate.boundValueOps(phone).set(code,5, TimeUnit.MINUTES);
            return R.success("验证码发送成功");
        }
        return R.error("验证码发送失败");
    }
    @PostMapping("/login")
    public R<User> login(@RequestBody Map<String,String> map, HttpServletRequest request){
        log.info(map.toString());
        String phone = map.get("phone").toString();
        String code = map.get("code").toString();
        //获取session中的验证码
        //String codeInSession = (String) request.getSession().getAttribute(phone);
        //从redis库中获取code
        Object codeInSession = redisTemplate.boundValueOps(phone).get();
        //判断传入的验证码是否与session中存储的一样，一样就登陆
        if(codeInSession!=null&&codeInSession.equals(code)){
            //判断该用户是否已注册
            LambdaQueryWrapper<User> lqw=new LambdaQueryWrapper<>();
            lqw.eq(User::getPhone,phone);
            User user = userService.getOne(lqw);
            if(user==null){//说明未注册，则对该用户进行注册
                user=new User();
                user.setStatus(1);
                user.setPhone(phone);
                userService.save(user);
            }
            //将该用户id存储到session中
            request.getSession().setAttribute("user",user.getId());
            redisTemplate.delete(phone);
            return R.success(user);
        }
        return R.error("登陆失败");
    }
}
